// sagas.js
import {all, call, put, takeEvery} from 'redux-saga/effects';
import actionTypes from '../actions';
import {Services} from '../../enums/constant';
import {API_BASE, API_PARAMS} from '../../enums/api';
import api from '../../services/axisos/apiPerson';

function* registerAsync(action: {type: String; payload: Person}) {
  try {
    const response: Response = yield call(() =>
      api.post(API_PARAMS.REGISTER, action.payload, {
        headers: {
          'Content-Type': 'application/json',
        },
      }),
    );
    console.log(response.data);
    if (response.data.code === 0) {
      if (response.data.data !== null) {
        yield put(
          actionTypes.register(
            response.data.data,
            action.payload,
            '注册成功，已登录',
          ),
        );
      } else {
        yield put(
          actionTypes.register(
            '',
            {phone: '', passwordcode: ''},
            '未知情况，联系开发人员',
          ),
        );
      }
    } else {
      yield put(
        actionTypes.register(
          '',
          {phone: '', passwordcode: ''},
          '注册失败，请检查用户名是否正确！',
        ),
      );
    }
  } catch (error) {
    // yield put(actionTypes.loginFailure(error));
  }
}

function* watchLogin() {
  yield takeEvery(Services.REGISTER_SERVICE, registerAsync);
}
export default function* rootSaga() {
  yield all([watchLogin()]);
}
